import os

labelimg_path = r"C:\Users\Administrator\Desktop\labelimg"
data_new_path = r"C:\Users\Administrator\Desktop\labelimg\data_new"
# 列出所有的图片名称
images = os.listdir(labelimg_path + "\images")
for i, image in enumerate(images):
    # 读取所有图片的数据
    image_data = open(labelimg_path + f"\images\{image}", "rb").read()
    # 将图像的名称改为一个新的名称并保存
    if os.path.exists(data_new_path+"\images"):
        open(data_new_path + f"\images\{i}.png", "wb").write(image_data)
    else:
        os.makedirs(data_new_path+"\images")
        open(data_new_path + f"\images\{i}.png", "wb").write(image_data)
# 列出所有标签名称
labels = os.listdir(labelimg_path + "\labels")
for i, label in enumerate(labels):
    # 读取所有标签文件的数据
    label_data = open(labelimg_path + f"\labels\{label}", "r+", encoding="utf-8").read()
    # 将文件的名称改为一个新的名称并保存
    if os.path.exists(data_new_path + "\labels"):
        open(data_new_path + f"\labels\{i}.txt", "w+", encoding="utf-8").write(label_data)
    else:
        os.makedirs(data_new_path + "\labels")
        open(data_new_path + f"\labels\{i}.txt", "w+", encoding="utf-8").write(label_data)
